package com.example.administrator.hqq.utils;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.activity.LogInActivity;
import com.example.administrator.hqq.broadcast.SealNotificationReceiver;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/14 14:12
 * @remark:
 */
public class RongIMUtils {
    /**
     * SUCCEED:融云链接成功
     * DEFEATED:融云链接失败
     * LOSE:验证失效
     */
    public enum Condition{
        SUCCEED,DEFEATED,LOSE
    }
    public static Condition condition = Condition.DEFEATED;
    public static void connect(String token, final Context context) {
        if (TextUtils.isEmpty(token))
        {
            return;
        }
        else{
            RongIM.getInstance().connect(token, new RongIMClient.ConnectCallback() {
                /**
                 * Token 错误。可以从下面两点检查 1.  Token 是否过期，如果过期您需要向 App Server 重新请求一个新的 Token
                 *                  2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
                 */
                @Override
                public void onTokenIncorrect() {
                    condition=Condition.LOSE;
                    ToastUtils.showShort("登录失败请稍后重试");
                    Intent intent = new Intent(context, LogInActivity.class);
                    context.startActivity(intent);
                }

                /**
                 * 连接融云成功
                 * @param userid 当前 token 对应的用户 id
                 */
                @Override
                public void onSuccess(String userid) {
                    Log.d("LoginActivity", "--onSuccess" + userid);
                    condition=Condition.SUCCEED;
                }

                /**
                 * 连接融云失败
                 * @param errorCode 错误码，可到官网 查看错误码对应的注释
                 */
                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                    condition = Condition.DEFEATED;
                }
            });
        }
    }
}
